<!DOCTYPE html>
<html xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>

<body>
<div class="layuimini-container" id="vue">
    <div class="layuimini-main">


        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">


        </script>
    </div>
</div>


<script src="../../lib/layui-v2.7.3/layui/layui.js" charset="utf-8"></script>
<script src="../../api/urlConfig.js"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script>





    layui.use(['table', 'util'], function(){
        var table = layui.table
            ,util = layui.util;

        //监听单元格编辑
        table.on('edit(currentTableId)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
        });
    });

    layui.use(['form', 'table','upload'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        table.render({
            elem: '#currentTableId',

            url: ipURL + "/caption/download/page",
            headers: {token: localStorage.token},
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": res.data.data,//解析数据列表

                };

            },

            response: {
                statusCode: 0//规定成功的状态码，默认：0
            },
            text: {none: '无数据'},
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {field: 'id', width: 80, title: 'ID', sort: true, hide: true},
                {field: 'taskName',title: '任务名称', },
                {field: 'fileSizeString', width: 200, title: '文件大小', sort: true},
                {field: 'size', width: 200, title: '文件数量', sort: true},
                {field: 'uploadWayName', width: 200, title: '下载方式'},
                {field: 'gmtCreated', width: 200, title: '下载时间'},
                {title: '操作', minWidth: 150, align: "center",
                    templet: function(d){
                    if (d.uploadWay === 0){
                        return `<a class="layui-btn layui-btn-xs layui-btn-normal data-count-delete" lay-event="download" ">下载</a>`;

                    }
                        return ``;
                    }}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var that = this;
            var data = obj.data;
            if(obj.event === 'download'){

                const url = ipURL + '/common/file/download/'+data.id; //记得拼接参数
                const xhr = new XMLHttpRequest();
                xhr.open('GET', url, true); // 也可以使用POST方式，根据接口
                xhr.setRequestHeader('token', localStorage.token); // 设置token
                xhr.setRequestHeader('Content-Type', 'application/octet-stream');
                xhr.responseType = 'blob'; // 返回类型blob
                xhr.onload = function(e) {
                    if (this.status === 200) {
                        const blob = this.response;
                        const reader = new FileReader();
                        reader.readAsDataURL(blob); // 转换为base64，可以直接放入a表情href
                        reader.onload = function(e) {
                            const a = document.createElement('a');
                            a.download = data.fileName;
                            a.href = e.target.result;
                            document.documentElement.appendChild(a);
                            a.click();
                            a.remove(); // 等价于document.documentElement.removeChild(a);
                        };
                    }
                };
                xhr.send(); // 发送ajax请求

            }
        })

    })


</script>

</body>

</html>